package fun.sanguo.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 *
 * @author ZYP
 * @date 2022-08-23
 */
public class DateUtils {

    /**
     * 日期格式
     */
    public static String DATE_FORMAT_01 = "yyyyMMddHHmmss";
    public static String DATE_FORMAT_02 = "yyyyMMddHHmm";
    public static String DATE_FORMAT_03 = "yyyyMMddHH";
    public static String DATE_FORMAT_04 = "yyyyMMdd";
    public static String DATE_FORMAT_05 = "yyyyMM";
    public static String DATE_FORMAT_06 = "yyyy";
    public static String DATE_FORMAT_07 = "MMdd";
    public static String DATE_FORMAT_08 = "MM";
    public static String DATE_FORMAT_09 = "dd";
    public static String DATE_FORMAT_10 = "HHmmss";
    public static String DATE_FORMAT_11 = "yyyy-MM-dd HH:mm:ss";
    public static String DATE_FORMAT_12 = "yyyy/MM/dd HH:mm:ss";
    public static String DATE_FORMAT_13 = "yyyy-MM-dd";
    public static String DATE_FORMAT_14 = "yyyy/MM/dd";
    public static String DATE_FORMAT_15 = "yyyy-MM";
    public static String DATE_FORMAT_16 = "yyyy/MM";

    /**
     * 获得某一格式的当前时间
     *
     * @param dateFormat
     * @return
     */
    public static String getTime(String dateFormat) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        return simpleDateFormat.format(new Date());
    }

    /**
     * 日期转换字符串
     *
     * @param date
     * @param dateFormat
     * @return
     */
    public static String dateToString(Date date, String dateFormat) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        return simpleDateFormat.format(date);
    }

    /**
     * 字符串转换日期
     *
     * @param dateStr
     * @param dateFormat
     * @return
     */
    public static Date stringToDate(String dateStr, String dateFormat) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        return simpleDateFormat.parse(dateStr);
    }

    /**
     * 获得某一格式的上月时间
     *
     * @param dateFormat
     * @return
     */
    public static String getLastMonth(String dateFormat) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        Date date = new Date();
        // 上月当前时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -1);
        return simpleDateFormat.format(calendar.getTime());
    }
}
